{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Elimination Matrices I: The Basics"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "n = 4"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "----------------\n",
        "Let's create an elimination matrix as $M$:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 24,
      "metadata": {
        "collapsed": false
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "array([[ 1.,  0.,  0.,  0.],\n",
              "       [ 2.,  1.,  0.,  0.],\n",
              "       [ 0.,  0.,  1.,  0.],\n",
              "       [ 0.,  0.,  0.,  1.]])"
            ]
          },
          "execution_count": 24,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "M = np.eye(n)\n",
        "M[1,0] = 2\n",
        "M"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Here's a matrix $A$. See if $M$ has the desired effect on $A$:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 19,
      "metadata": {
        "collapsed": false
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "array([[ 0.4, -0.3,  2.4, -0.3],\n",
              "       [ 0.1,  1.6, -0.9, -0.6],\n",
              "       [ 0.2, -0.3, -1.2, -0.2],\n",
              "       [-0.4,  0.6, -1.7, -0.7]])"
            ]
          },
          "execution_count": 19,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "np.random.seed(5)\n",
        "A = np.random.randn(n, n).round(1)\n",
        "A"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 20,
      "metadata": {
        "collapsed": false
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "array([[ 0.4, -0.3,  2.4, -0.3],\n",
              "       [ 0.9,  1. ,  3.9, -1.2],\n",
              "       [ 0.2, -0.3, -1.2, -0.2],\n",
              "       [-0.4,  0.6, -1.7, -0.7]])"
            ]
          },
          "execution_count": 20,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "M.dot(A)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "-----------------------\n",
        "Next, see if you can build the inverse of $M$:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 25,
      "metadata": {
        "collapsed": false
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "array([[ 1.,  0.,  0.,  0.],\n",
              "       [-2.,  1.,  0.,  0.],\n",
              "       [ 0.,  0.,  1.,  0.],\n",
              "       [ 0.,  0.,  0.,  1.]])"
            ]
          },
          "execution_count": 25,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "Minv = np.eye(n)\n",
        "Minv[1,0] = -2\n",
        "Minv"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 26,
      "metadata": {
        "collapsed": false
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "array([[ 1.,  0.,  0.,  0.],\n",
              "       [ 0.,  1.,  0.,  0.],\n",
              "       [ 0.,  0.,  1.,  0.],\n",
              "       [ 0.,  0.,  0.,  1.]])"
            ]
          },
          "execution_count": 26,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "M.dot(Minv)"
      ]
    }
  ],
  "metadata": {},
  "nbformat": 4,
  "nbformat_minor": 0
}